Remember?
Register
Back

Templating

By Monkeymatt last edited on Saturday, May 20, 2006 at 1:11:02 pm by Monkeymatt. Page 2.

<< Prev123456789Next >>

Here we will begin figuring how to take the simple, non-dynamic HTML file and make it dynamic.

First, we will need to make the base of a class to be used. (Now would be a good time to note that this tutorial is based on PHP5):
PHP Code

<?php
class Template{
    
// Define global variables
    
private $code=array(); // This will hold all of the code we get and all of the sections in a multidimensional array
    
private $file_root// Holds the main directory where the template files are stored, defined in constructor
    
    // Generic error function to help make error reporting easier
    
protected function error($function$message) {
        die(
"<b>Error</b><br />Function: $function<br />Message:$message");
    }
    
    
// Constructor, takes in file root and sets it in the global variable
    
public function __construct($file_root="./") {
        if (
$file_root == "") {
            
$file_root="./";
        } else if (!
is_dir($file_root)) {
            
$this->error("Constructor""$file_root is not a directory"); // Throw an error if the directory does not exist
        
}
        
// Now there are no errors
        
$this->file_root=$file_root// Set global variable
        
$this->code['base']['****code****']=''// For later
    
}
}
?>




The two global variables will be explained more later, but you can get the gist of what they are doing right now. The error function simply dies and prints out an error message when an error occurrs in the template parsing. The __construct function is the constructor -- it will run when an instance of the class is made, and will take the input, in this case the file root, and setup variables to be used elsewhere. It does that here by making sure we have a valid directory to pull template files from and setting up the code directory to be able to hold the code from the template file.

Next we will start adding files and a few more complex variables to the class.

<< Prev123456789Next >>